package Java实训.第五天异常和IO流;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class 薪资统计 {
    /*
    * 统计”C:\Users\gdq\OneDrive\桌面\File文件夹\task-20221125.doc“文件中
    * 不同职位的平均薪资
    */
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("C:\\Users\\gdq\\OneDrive\\桌面\\File文件夹\\task-20221125.txt");
        HashMap<String, Integer> map = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        int b;
        while ((b = fr.read())!=-1){
            if (b == 10){
                fr.read();fr.read();
                String[] split = builder.toString().split(",");
                String[] str =split[4].split("-");
                int xz = (Integer.parseInt(str[1])+Integer.parseInt(str[0])) / 2;
                if (map.containsKey(split[1])){
                   int value = (map.get(split[1]) + xz) / 2;
                   map.put(split[1], value);
                }else {
                    map.put(split[1], xz);
                }
                builder.delete(0,builder.length()-1);
            }
            builder.append((char) b);
        }
        fr.close();
        System.out.println(map);
    }
}
